package com.google.code.greenplate;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;



public class RideOfferFeedHandler extends DefaultHandler {

    StringBuilder sb = null;
    String ret = "";
    boolean bStore = false;
    int howMany = 0;


    RideOfferFeedHandler() {
    }

    String getResults()
    {
    	//return "XML parsed data.\nThere are [" + howMany + "] status updates\n\n" + ret;
    	return "XML parsed data.\nThere are [" + howMany + "] transport updates\n\n" + ret;
    }
    @Override

    public void startDocument() throws SAXException {
    	// initialize "list"
    }

    @Override
    public void endDocument() throws SAXException {

    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

        try {
        	if (localName.equals("transport")) {
        		this.sb = new StringBuilder("");
        		bStore = true;
        	}
        	if (localName.equals("transportDescription")) {
        		this.sb = new StringBuilder("");
        	}
        	if (localName.equals("transportName")) {
        		this.sb = new StringBuilder("");
        	}
        	if (localName.equals("type")) {
        		this.sb = new StringBuilder("");
        	}
        } catch (Exception ee) {

            Log.d("error in startElement", ee.getStackTrace().toString());
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

    	if (bStore) {
	        if (localName.equals("transportDescription")) {	
	        	ret += "Description: " + sb.toString() + "\n"; 
	        	sb = new StringBuilder("");
	            return;	
	        }	
	        if (localName.equals("transportName")) {	
	        	ret += "Name: " + sb.toString() + "\n";
	        	sb = new StringBuilder("");
	        	return;	
	        }
	        if (localName.equals("type")) {	
	        	ret += "Type: " + sb.toString() + "\n\n";
	        	sb = new StringBuilder("");
	        	return;	
	        }
    	}
        if (localName.equals("transport")) {
        	howMany++;
        	bStore = false;
        }
    }
    
    /*
    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

        try {
        	if (localName.equals("status")) {
        		this.sb = new StringBuilder("");
        		bStore = true;
        	}
        	if (localName.equals("user")) {
	        	bStore = false;
	        }
        	if (localName.equals("text")) {
        		this.sb = new StringBuilder("");
        	}
        	if (localName.equals("created_at")) {
        		this.sb = new StringBuilder("");
        	}
        } catch (Exception ee) {

            Log.d("error in startElement", ee.getStackTrace().toString());
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

    	if (bStore) {
	        if (localName.equals("created_at")) {	
	        	ret += "Date: " + sb.toString() + "\n"; 
	        	sb = new StringBuilder("");
	            return;	
	        }
	
	        if (localName.equals("user")) {
	        	bStore = true;
	        }
	
	        if (localName.equals("text")) {	
	        	ret += "Post: " + sb.toString() + "\n\n";
	        	sb = new StringBuilder("");
	        	return;	
	        }
    	}
        if (localName.equals("status")) {
        	howMany++;
        	bStore = false;
        }
    }
    */


    @Override

    public void characters(char ch[], int start, int length) {

    	if (bStore) {
	        String theString = new String(ch, start, length);
	
	        this.sb.append(theString);
    	}
    }

}

